﻿#include <qg/qg.h>

using namespace tpl;

static float Angles[] =
{
	-8.65f,
	5.95f,		// ch
	20.5f,		// 10
	35.2f,		// 5
	50.3f,		// 3
	65.15f,		// 2
	80.54f,		// 1
	94.76f,		// ch
	110.0f,		// 10
	125.0f,		// 5
	140.0f,		// 3
	155.0f,		// 2
	170.0f,		// 1
};

static const char* Strs[] =
{
	"CH",
	"10",
	"5",
	"3",
	"2",
	"1",
	"CH",
	"10",
	"5",
	"3",
	"2",
	"1",
};

static const float SDeg[] =
{
	2.5f,
	2.5f + 14.55f,
	2.5f + 14.55f + 14.7f,
	2.5f + 14.55f + 14.7f + 15.1f,
	2.5f + 14.55f + 14.7f + 15.1f + 14.85f,
	2.5f + 14.55f + 14.7f + 15.1f + 14.85f + 15.22,
};

float ff = 0.0f, spd = 0.0f, btm = 0.0f;
bool isbrk = false;

void resetroll(int n)
{
	ff = k_torad(SDeg[n]);
	spd = KM_PI_2;

	isbrk = !isbrk;
	btm = 0.0f;
}

bool keydown(qgSevKey& key)
{
	switch (key.key)
	{
		case QIK_1:
			spd = 0.0f;
			break;

		case QIK_2:
			spd = KM_PI_HALF / 16.0f;
			break;

		case QIK_3:
			spd = KM_PI_HALF / 8.0f;
			break;

		case QIK_4:
			spd = KM_PI_HALF / 2.0f;
			break;

		case QIK_5:
			spd = KM_PI_HALF;
			break;

		case QIK_0:
			ff = 0.0f;
			spd = KM_PI_2;
			break;

		case QIK_SPACE:
			ff = k_torad(SDeg[3]);
			spd = KM_PI_2;

			isbrk = !isbrk;
			btm = 0.0f;
			break;

		case QIK_Z: resetroll(0); break;
		case QIK_X: resetroll(1); break;
		case QIK_C: resetroll(2); break;
		case QIK_V: resetroll(3); break;
		case QIK_B: resetroll(4); break;
		case QIK_N: resetroll(5); break;
	}

	return true;
}

int main(void)
{
	k_mesg("RECT ROTATION\n");

	qgDevice* dev = qgDevice::New("", 800, 800, QGSDF_LAYOUT | QGSDF_AUTO);

	if (dev)
	{
		dev->Mount(QG_WORK_PATH);

		qgStub* stub = dev->GetStub();
		stub->AddKeyDownHandler(qgSevKeyFunc(&keydown));

		qgFreeType* font = qgFreeType::New();

		qgTexture* tex = qgTexture::New("wof2.png", QGTEXF_S2D);

		//
		while (dev->Run())
		{
			float adv = stub->GetAdvance();

			if (isbrk)
			{
				btm += adv;

				if (btm > 3.0f)
				{
					spd -= spd * 0.5f * adv;

					if (k_todeg(spd) < 2.0f)
					{
						spd = 0.0f;
						isbrk = false;
					}
				}
			}

			ff += spd * adv;
			ff = k_cprad(ff);

			kint idx = 0;
			float df = k_todeg(ff);
			float dh = df - 8.65f > 170.0f ? df - 178.65f : df;

			for (kint i = 0; i < K_COUNTOF(Angles) - 1; i++)
			{
				if (dh >= Angles[i] && dh <= Angles[i + 1])
				{
					idx = i;
					break;
				}
			}

			const char* msg = idx >= 0 && idx < K_COUNTOF(Strs) ? Strs[idx] : "!!!!!";

			dev->Enter();

			Rect rt(Point(0, 0), Point(tex->GetWidth(), tex->GetHeight()));
			dev->DrawRect(rt, tpl::ColorValues::White, tex);
			dev->DrawRotationRect(-ff, rt, tpl::ColorValue(255, 255, 255, 180), tex);

			dev->DrawRect(Rect(Point(512, 0), Point(256, 256)), tpl::ColorValues::White, tex);

			Point pts[] = {Point(256, 0), Point(256, 512)};
			ColorValue crs[] = {ColorValues::White, ColorValues::White};
			dev->DrawPolyLine(2, pts, crs);

			font->SetSize(16);
			font->WriteFormat(5, 5, 0, "[%.2f] %.3f (%.0f) | %.2f, %.2f", stub->GetRunFps(), ff, k_todeg(ff), k_todeg(spd), btm);

			font->SetSize(48);
			font->WriteFormat(512, 400, 0, "%d - %s", idx, msg);

			dev->Leave();
			dev->Flush();
		}

		font->Unload();
		dev->Unload();
	}

	return 0;
}

